#include "main.h"
#include "Functions.h"
#include "Global.h"

void DrawTileset(CSprite *sprite, int xframe, int yframe, Vector2 pos, Vector2 scale, float angle, D3DCOLOR argb){
	angle*= -1;
	RECT rct;

	if (xframe<1){xframe=1;}
	if (yframe<1){yframe=1;}

	rct.right=(long)sprite->xtile*xframe;
	rct.left=(long)(sprite->xtile*xframe)-(long)(sprite->xtile);
	rct.bottom=(long)(sprite->ytile*yframe);
	rct.top=(long)(sprite->ytile*yframe)-(long)(sprite->ytile);

	angle = min(max(angle, -360), 360);
	angle = D3DXToRadian(angle);
	
	if(g_BlendType==1){//Add
		g_pD3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
		g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
		g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
	}else if(g_BlendType==2){//Subtract
		g_pD3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_SUBTRACT);
		g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
		g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
	}
	D3DXMatrixScaling(&sprite->scale, scale.x,scale.y,1.0f);
	D3DXMatrixTranslation(&sprite->trans,pos.x-CAMERA.x,pos.y-CAMERA.y,0);
	D3DXMatrixRotationZ(&sprite->rot, angle);

	sprite->final = sprite->scale * sprite->rot * sprite->trans;
	g_pDrawSprite->SetTransform(&sprite->final);
	g_pDrawSprite->Draw(sprite->texture, &rct, &sprite->center, NULL, argb);

}
void DrawSprite(CSprite *sprite, int frame, Vector2 pos){
	DrawSpriteExact(sprite, frame, V(pos.x-CAMERA.x, pos.y-CAMERA.y), V(1, 1), 0, CLR_WHITE);
}
void DrawSprite(CSprite *sprite, int frame, Vector2 pos, Vector2 scale, float angle, D3DCOLOR argb){
	DrawSpriteExact(sprite, frame, V(pos.x-CAMERA.x, pos.y-CAMERA.y), scale, angle, argb);	
}

void DrawRect(Vector2 pos, int w, int h, D3DCOLOR color){
	DrawRectExact(V(pos.x-CAMERA.x-(w/2), pos.y-CAMERA.y-(h/2)), w, h, color);
}

void DrawRect(Vector2 pos, int w, int h, D3DCOLOR color, int bordersize, D3DCOLOR bordercolor){
	DrawRectExact(V(pos.x-CAMERA.x-(w/2), pos.y-CAMERA.y-(h/2)), w, h, color, bordersize, bordercolor);
}

void DrawText(CText* font, std::string str, Vector2 pos, D3DCOLOR color){
	DrawTextExact(font, str, V(pos.x-CAMERA.x, pos.y-CAMERA.y), color);
}
void DrawText(CText* font, std::string str, Vector2 pos, float width, float height, D3DCOLOR color, DWORD flags){
	DrawTextExact(font, str, V(pos.x-CAMERA.x, pos.y-CAMERA.y), width, height, color, flags);
}

void DrawLine(Vector2 pos1, Vector2 pos2, D3DCOLOR color){
	DrawLineExact(pos1-CAMERA, pos2-CAMERA, color);
}

/*---------------------------------------------------------
-Exact
---------------------------------------------------------*/
void DrawTextExact(CText* font, std::string str, Vector2 pos, D3DCOLOR color){
	if(g_BlendType==1){//Add
		g_pD3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
		g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
		g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
	}else if(g_BlendType==2){//Subtract
		g_pD3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_SUBTRACT);
		g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
		g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
	}
	font->Draw(str.c_str(), pos.x, pos.y, color);
}
void DrawTextExact(CText* font, std::string str, Vector2 pos, float width, float height, D3DCOLOR color, DWORD flags){
	if(g_BlendType==1){//Add
		g_pD3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
		g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
		g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
	}else if(g_BlendType==2){//Subtract
		g_pD3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_SUBTRACT);
		g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
		g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
	}
	font->Draw(str.c_str(), pos.x, pos.y, width, height, color, flags);
}

void DrawLineExact(Vector2 pos1, Vector2 pos2, D3DCOLOR color){
	if(g_BlendType==1){//Add
		g_pD3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
		g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
		g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
	}else if(g_BlendType==2){//Subtract
		g_pD3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_SUBTRACT);
		g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
		g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
	}
	linepts[0]=pos1;
	linepts[1]=pos2;
	g_pDrawLine->Draw(linepts, 2, color);
}
void DrawSpriteExact(CSprite *sprite, int frame, Vector2 pos, Vector2 scale, float angle, D3DCOLOR argb){
	angle*= -1;
	++frame;
	RECT rct;
	rct.left=0;
	rct.top=0;
	rct.bottom=(long)sprite->height;

	if (frame>sprite->frames){frame=sprite->frames;}
	if (frame<0){frame=0;}

	if (frame == 0){
		
		rct.right=(long)sprite->xtile*sprite->count;
		rct.left=(long)(sprite->xtile*sprite->count)-(long)(sprite->xtile);
		if (sprite->count <= sprite->frames){
			if (sprite->imgspeed < sprite->speed){
				sprite->imgspeed++;
			}else{
				sprite->count++;
				sprite->imgspeed=0;
			}
		}
		if (sprite->count > sprite->frames){
			sprite->count=1;
		}
		
	}else{
		rct.right=(long)sprite->xtile*frame;
		rct.left=(long)(sprite->xtile*frame)-(long)(sprite->xtile);
	}
	angle = min(max(angle, -360), 360);
	angle = D3DXToRadian(angle);
	
	if(g_BlendType==1){//Add
		g_pD3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
		g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
		g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
	}else if(g_BlendType==2){//Subtract
		g_pD3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_SUBTRACT);
		g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
		g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
	}
	D3DXMatrixScaling(&sprite->scale, scale.x,scale.y,1.0f);
	D3DXMatrixTranslation(&sprite->trans,pos.x,pos.y,0);
	D3DXMatrixRotationZ(&sprite->rot, angle);

	sprite->final = sprite->scale * sprite->rot * sprite->trans;
	g_pDrawSprite->SetTransform(&sprite->final);
	g_pDrawSprite->Draw(sprite->texture, &rct, &sprite->center, NULL, argb);
}
void DrawSpriteExact(CSprite *sprite, int frame, Vector2 pos){
	DrawSpriteExact(sprite, frame, pos, V(1,1), 0, CLR_WHITE);
}

void DrawRectExact(Vector2 pos, int w, int h, D3DCOLOR color){
	RECT rct;
	rct.left=0;
	rct.top=0;
	rct.bottom=h;
	rct.right=w;
	D3DXMATRIX trans;
	D3DXMatrixIdentity(&trans);
	D3DXMatrixTranslation(&trans,pos.x,pos.y,0);
	if(g_BlendType==1){//Add
		g_pD3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
		g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
		g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
	}else if(g_BlendType==2){//Subtract
		g_pD3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_SUBTRACT);
		g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
		g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
	}
	g_pDrawSprite->SetTransform(&trans);
	g_pDrawSprite->Draw(g_pSpriteList->Misc->Rect->texture, &rct, NULL, NULL, color);
}

void DrawRectExact(Vector2 pos, int w, int h, D3DCOLOR color, int bordersize, D3DCOLOR bordercolor){
	if(bordersize==0)
		DrawRectExact(pos, w, h, color);
	else{
		RECT rct;
		rct.left=bordersize;
		rct.top=bordersize;
		rct.bottom=h-bordersize;
		rct.right=w-bordersize;

		D3DXMATRIX trans;
		D3DXMatrixIdentity(&trans);
		D3DXMatrixTranslation(&trans,(pos.x+bordersize),(pos.y+bordersize),0);
		if(g_BlendType==1){//Add
			g_pD3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
			g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
			g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
		}else if(g_BlendType==2){//Subtract
			g_pD3DDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_SUBTRACT);
			g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
			g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
		}
		g_pDrawSprite->SetTransform(&trans);
		g_pDrawSprite->Draw(g_pSpriteList->Misc->Rect->texture, &rct, NULL, NULL, color);
		rct.left=0;
		rct.top=0;
		rct.right=w;
		rct.bottom=bordersize;
		D3DXMatrixTranslation(&trans,pos.x,pos.y,0);
		g_pDrawSprite->SetTransform(&trans);
		g_pDrawSprite->Draw(g_pSpriteList->Misc->Rect->texture, &rct, NULL, NULL, bordercolor);
		D3DXMatrixTranslation(&trans,pos.x,pos.y+(float)(h-bordersize),0);
		g_pDrawSprite->SetTransform(&trans);
		g_pDrawSprite->Draw(g_pSpriteList->Misc->Rect->texture, &rct, NULL, NULL, bordercolor);
		rct.bottom=h-(bordersize*2);
		rct.right=bordersize;
		D3DXMatrixTranslation(&trans,pos.x,pos.y+(float)(bordersize),0);
		g_pDrawSprite->SetTransform(&trans);
		g_pDrawSprite->Draw(g_pSpriteList->Misc->Rect->texture, &rct, NULL, NULL, bordercolor);
		D3DXMatrixTranslation(&trans,pos.x+(float)(w-bordersize),pos.y+(float)(bordersize),0);
		g_pDrawSprite->SetTransform(&trans);
		g_pDrawSprite->Draw(g_pSpriteList->Misc->Rect->texture, &rct, NULL, NULL, bordercolor);
	}
}